﻿Public Class frmMeetingsModify


    Dim strNameForm As String = "Modify Meeting"
    Dim mID As Integer

    Public Function Initialize(ByVal inID As Integer) As Boolean
        Try
            Me.Text = strNameForm
            mID = inID

            cboTopics.Load("Select * From Topics", "Name", gConnectionStringAgenda.ToString)
            cboUS.Load("Select * From Sprints", "Name", gConnectionStringAgenda.ToString)
            Dim listStatus As New List(Of Base.GlobalConst)
            listStatus.Add(MEETING_STATUS_PLANNED)
            listStatus.Add(MEETING_STATUS_DONE)
            listStatus.Add(MEETING_STATUS_CANCELLED)
            cboStatus.DataSource = listStatus
            cboStatus.ValueMember = "NUMBER"
            cboStatus.DisplayMember = "NAME"
            listStatus = Nothing
            cboStatus.SelectedIndex = -1

            AddHandler cboTopics.OnInsert, AddressOf InterfaceManager.Topics_OnInsert
            AddHandler cboTopics.OnModify, AddressOf InterfaceManager.Topics_OnModify
            AddHandler cboUS.OnInsert, AddressOf InterfaceManager.UserStories_OnInsert
            AddHandler cboUS.OnModify, AddressOf InterfaceManager.UserStories_OnModify

            If Not LoadForm() Then
                MsgBox("Impossible open form.", vbExclamation, Me.Text)
                Return False
            End If
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "LoadForm"

    Private Function LoadForm() As Boolean
        Try
            Dim oTMeetings As New Data.TMeetings
            If oTMeetings.LoadData(mID) Then
                txtName.Text = oTMeetings.mName
                txtDescription.Text = oTMeetings.mDescription
                txtDate.Data = oTMeetings.mDate
                txtTime.Time = oTMeetings.mTime
                cboTopics.SelectedValue = oTMeetings.mTopicsID
                cboUS.SelectedValue = oTMeetings.mUserStoriesID
                cboStatus.SelectedValue = CInt(oTMeetings.mStatus)

            Else
                Return False
            End If
            oTMeetings = Nothing
            Return True
        Catch ex As Exception
            Base.ShowError.Show("Carica dati", Me.Name, "LoadForm", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdModify_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdModify.Click
        Try
            If IsComplete() Then
                Dim oTMeetings As New Data.TMeetings
                If oTMeetings.LoadData(mID) Then
                    oTMeetings.mName = txtName.Text
                    oTMeetings.mDescription = txtDescription.Text
                    oTMeetings.mDate = txtDate.DataMySql
                    oTMeetings.mTime = txtTime.Time
                    oTMeetings.mTopicsID = cboTopics.SelectedValue
                    oTMeetings.mUserStoriesID = cboUS.SelectedValue
                    oTMeetings.mStatus = cboStatus.SelectedValue

                    If oTMeetings.Modify() Then
                        Me.DialogResult = Windows.Forms.DialogResult.OK
                    Else
                        MsgBox("Impossible modify the meeting.", vbExclamation, Me.Text)
                        Me.DialogResult = Windows.Forms.DialogResult.Cancel
                    End If
                Else
                    MsgBox("Impossible modify the meeting.", vbExclamation, Me.Text)
                    Me.DialogResult = Windows.Forms.DialogResult.Cancel
                End If
            End If
        Catch ex As Exception
            Base.ShowError.Show("Modify", Me.Name, "cmdModify_Click", ex)
        End Try
    End Sub

#End Region

#Region "Other functions"

    Private Function IsComplete() As Boolean
        Try
            If txtDescription.Text = "" Then
                MsgBox("Insert description.", vbExclamation, Me.Name)
                txtDescription.Focus()
                Return False
            End If
            If txtDate.Data = "" Then
                MsgBox("Select date.", vbExclamation, Me.Name)
                txtDate.Focus()
                Return False
            End If
            If txtTime.Time = "" Then
                MsgBox("Select time.", vbExclamation, Me.Name)
                txtDate.Focus()
                Return False
            End If

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Insertion", Me.Name, "IsComplete", ex)
            Return False
        End Try
    End Function

#End Region

#Region "Form events"

    Private Sub frmTableSingleInsertion_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class